<template>
  <div class="login-page">
    <div class="login-wrapper">
      <main class="login-content">
        <div class="login-body">
          <h3 class="login-title">ARTA能源管控平台</h3>
          <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" status-icon>
            <el-form-item prop="username">
              <el-input v-model="dataForm.username" :placeholder="$t('login.username')">
                <span slot="prefix" class="el-input__icon">
                  <svg class="icon-svg" aria-hidden="true">
                    <use xlink:href="#icon-user"></use>
                  </svg>
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="dataForm.password" type="password" :placeholder="$t('login.password')">
                <span slot="prefix" class="el-input__icon">
                  <svg class="icon-svg" aria-hidden="true">
                    <use xlink:href="#icon-lock"></use>
                  </svg>
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="captcha">
              <el-row :gutter="20">
                <el-col :span="14">
                  <el-input v-model="dataForm.captcha" :placeholder="$t('login.captcha')">
                    <span slot="prefix" class="el-input__icon">
                      <svg class="icon-svg" aria-hidden="true">
                        <use xlink:href="#icon-safetycertificate"></use>
                      </svg>
                    </span>
                  </el-input>
                </el-col>
                <el-col :span="10" class="login-captcha">
                  <img :src="captchaPath" @click="getCaptcha()">
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="dataFormSubmitHandle()" class="login-btn">{{ $t('login.title') }}</el-button>
            </el-form-item>
          </el-form>
        </div>
      </main>
    </div>
  </div>
</template>

<script>
import debounce from "lodash/debounce";
import { messages } from "@/i18n";
import { getUUID } from "@/utils";
export default {
  data() {
    return {
      i18nMessages: messages,
      captchaPath: "",
      dataForm: {
        username: "",
        password: "",
        uuid: "",
        captcha: ""
      }
    };
  },
  computed: {
    dataRule() {
      return {
        username: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        captcha: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ]
      };
    }
  },
  created() {
    this.getCaptcha();
  },
  methods: {
    // 获取验证码
    getCaptcha() {
      this.dataForm.uuid = getUUID();
      this.captchaPath = `${window.SITE_CONFIG["apiURL"]}/captcha?uuid=${
        this.dataForm.uuid
      }`;
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http
            .post("/login", this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                this.getCaptcha();
                return this.$message.error(res.msg);
              }
              sessionStorage.setItem("token", res.data.token);
              this.$router.replace({ name: "home" });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  }
};
</script>
<style lang="scss">

.login-page::before {
  background-image: url(~@/assets/img/login.jpg);
  background-size: cover;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -1;
  content: "";
}

.login-wrapper {
  height: 100vh;
  background-color: transparent;
  min-height: 100vh;
  margin-left: 0;
  overflow-x: hidden;
  overflow-y: auto;
}

.login-content {
  background: #ffffff;
  border-radius: 4px;
  width: 476px;
  height: 462px;
  position: absolute;
  top: 50%;
  right: 110px;
  margin: -231px 0 0 0;
  
}

.login-body {
  padding: 46px 57px 70px 51px;
}
.login-title {
  text-align: center;
  font-size: 30px;
  color: #31CFBA;
}

.login-captcha>img {
    max-width: 100%;
    cursor: pointer;
}

.login-btn{
  width: 100%;
  background-color: #31CFBA;
  border-color: #31CFBA;
}


</style>>
